package com.sysadmin.action;

import java.io.IOException;

import com.common.base.BaseAction;
import com.sysadmin.service.LoginService;
import com.sysadmin.service.MenuService;
import com.sysadmin.service.UserService;
import com.sysadmin.vo.UsersVO;
public class LoginAction extends BaseAction {
	
	String username;
	String password;
	String randcode;
	String id;
	String oldpassword;
	LoginService service = new LoginService();
	UserService userService = new UserService();


	public String login() {
		String rand = (String) this.getSession().getAttribute("rand");
		if(rand!=null&&rand.equals(randcode)){
			UsersVO vo = new UsersVO();
			vo.setUsername(username);
			vo.setPassword(password);
			
			boolean isLogin = service.login(vo);
			if(isLogin){
				this.getSession().setAttribute("users", vo);
				MenuService menuService = new MenuService();
				String treeStr = menuService.getMenuObject4User(vo,this.getRequest());
				log.info("treeStr========"+treeStr);
				this.getSession().setAttribute("treeStr", treeStr);
				return topager("/index.jsp");
			}else{
				this.getRequest().setAttribute("msg", "用户名密码错误！");
				return topager("/login.jsp");
			}
		}else{
			this.getRequest().setAttribute("msg", "验证码出错！");
			return topager("/login.jsp");
		}
	}
	public String logout() throws IOException{
		this.getSession().removeAttribute("users");
		return redirect("/login.jsp");
	}
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRandcode() {
		return randcode;
	}

	public void setRandcode(String randcode) {
		this.randcode = randcode;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getOldpassword() {
		return oldpassword;
	}

	public void setOldpassword(String oldpassword) {
		this.oldpassword = oldpassword;
	}

	public LoginService getService() {
		return service;
	}

	public void setService(LoginService service) {
		this.service = service;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	
}
